package com.bicon.botu.file.upload.api;

import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.file.upload.common.FileUploadConstants;
import com.bicon.botu.file.upload.service.FileUploadService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.bicon.botu.file.upload.vo.FileInfo;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * 文件上传
 *
 * @author tandk
 * @date 2017/8/2 0002 上午 11:36
 */
@RequestMapping("/V1.0.0/api/file")
@RestController
public class FileUploadApi extends BaseController {

    @Autowired
    FileUploadService fileUploadService;

    /**
     * 上传文件
     *
     * @param request request
     * @param appName 调用的服务id
     * @param module  调用的服务名
     * @param width   压缩宽度
     * @param height  压缩高度
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public BaseResult upload(HttpServletRequest request,
                             @RequestParam("appId") String appId,
                             @RequestParam("appName") String appName,
                             @RequestParam("module") String module,
                             @RequestParam("business") String business,
                             @RequestParam(required = false) String entityId,
                             @RequestParam(defaultValue = "0", required = false) Integer width,
                             @RequestParam(defaultValue = "0", required = false) Integer height,
                             @RequestParam(value = "type", required = false) String type) throws IOException {
        if (StringUtils.isAnyBlank(appId, appName, module, business)) {
            return sendResult400();
        }
        if (StringUtils.isNotBlank(type)) {
            if (!FileUploadConstants.UPLOAD_FILE_TYPE.equals(type)) {
                return sendResult400("传参错误");
            }
        }
        List<FileInfo> fileList = fileUploadService.doUpload(request, appId, appName, module, business, entityId, width, height, type);
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, fileList);
    }

    /**
     * 上传文件（直接返回图片地址）
     *
     * @param request request
     * @param appName 调用的服务id
     * @param module  调用的服务名
     * @param width   压缩宽度
     * @param height  压缩高度
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "uploadWebEditor", method = RequestMethod.POST)
    public String uploadWebEditor(HttpServletRequest request,
                                  @RequestParam("appId") String appId,
                                  @RequestParam("appName") String appName,
                                  @RequestParam("module") String module,
                                  @RequestParam("business") String business,
                                  @RequestParam(required = false) String entityId,
                                  @RequestParam(defaultValue = "0", required = false) Integer width,
                                  @RequestParam(defaultValue = "0", required = false) Integer height,
                                  @RequestParam(value = "type", required = false) String type) throws IOException {
        if (StringUtils.isAnyBlank(appId, appName, module, business)) {
            return "传参错误";
        }
        if (StringUtils.isNotBlank(type)) {
            if (!FileUploadConstants.UPLOAD_FILE_TYPE.equals(type)) {
                return "传参错误";
            }
        }
        List<FileInfo> fileList = fileUploadService.doUpload(request, appId, appName, module, business, entityId, width, height, type);
        if (fileList.size() > 0) {
            return fileList.get(0).getPath();
        }
        return "";
    }

    /**
     * 删除文件
     *
     * @param filePath 文件路径
     * @return
     */
    @RequestMapping(value = "/deleteFile", method = RequestMethod.POST)
    public BaseResult deleteFile(@RequestParam String filePath) {
        if (StringUtils.isAnyBlank(filePath)) {
            return sendResult400();
        }
        boolean b = fileUploadService.deleteFile(filePath);
        if (b) {
            return sendResult200();
        } else {
            return sendResult500("文件不存在");
        }
    }
}
